<template>
  <div>
     <van-nav-bar title="我的收货地址" fixed placeholder left-arrow  @click-left="onClickLeft"/>

    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />     


  </div>
</template>

<script>
import { getAddressApi } from "@/utils/api";
// import { mapState } from "vuex";
import { Toast } from "vant";
export default {
  data() {
    return {
      chosenAddressId: "1",
      list: [],
      newAddress: {},
      id: 1
    };
  },

  components: {},
  // computed: mapState(["userAddress"]),
  created() {
    this.getaddress();
  },

  methods: {
    onAdd() {
      Toast("新增地址");
      this.$router.push("/addaddress");
    },
    onEdit(item, index) {
      console.log(item);
      Toast("编辑地址:" + index);
      this.$store.commit("updateAddress", item);
      this.$router.push("/updateaddress");
    },
    getaddress() {
      getAddressApi({ token: localStorage.getItem("token") }).then(res => {
        if (res.status === "0") {
          // console.log(res.result.resArr);
          res.result.resArr.forEach(el => {
            // console.log(el);
            this.newAddress.id = this.id++;
            this.newAddress.addressId = el.addressId;
            this.newAddress.name = el.getName;
            this.newAddress.tel = el.getPhone;
            this.newAddress.address = el.address;
            this.newAddress.isDefault = el.status == 1 ? true : false;
            this.list.push(this.newAddress);
            this.newAddress = {};
          });
          console.log(this.list);
        }
      });
    },
    onClickLeft() {
      this.$router.go(-1);
    },
  }
};
</script>

<style lang="less" scoped></style>
